package org.jha.relay.krxy;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Arrays;

public class K8623 {

	public static void main(String args[]) throws InterruptedException {
		K8623 k = new K8623("192.168.1.159", 8000);
		info("--------writeDelay true---------");
		k.writeDelay(8, true);
		info("--------readDelay---------");
		info("readDelay:" + k.readDelay(8));
		info("--------readOnOff---------");
		info("readOnOff:" + k.readOnOff(8));
		info("--------writeDelay false---------");
		for (int i = 0; i <= 8; i++) {
			k.writeDelay(i, false);
//			Thread.sleep(1000);
		}
	}

	private static void debug(String string) {
//		System.out.println(string);
	}
	private static void info(String string) {
		System.out.println(string);
	}

	private String ip;
	private int port;

	public K8623(String ip, int port) {
		super();
		this.ip = ip;
		this.port = port;
	}

	private static final byte[] READ_DELAY = new byte[] { (byte) 0x00,
			(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06,
			(byte) 0x02, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00,
			(byte) 0x08 };
	private static final byte[] READ_ONOFF = new byte[] { (byte) 0x00,
			(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x06,
			(byte) 0x02, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00,
			(byte) 0x08 };

	// read output
	public boolean readDelay(int bit) {
		if (bit < 1 && bit > 8) {
			throw new IllegalArgumentException("wrong bit(1-8):" + bit);
		}
		byte[] data = socketCmd(READ_DELAY);
		return byteTo8Bit(data[9]).charAt(8-bit) == '1';
	}

	public void writeDelay(int bit, boolean value) {
		if (bit < 1 && bit > 8) {
			throw new IllegalArgumentException("wrong bit(1-8):" + bit);
		}
		byte[] cmd = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00,
				(byte) 0x00, (byte) 0x00, (byte) 0x06, (byte) 0x02,
				(byte) 0x05, (byte) 0x00, (byte) (bit - 1),
				value ? (byte) 0xff : (byte) 0x00, (byte) 0x00 };
		socketCmd(cmd);
	}

	// read input
	public boolean readOnOff(int bit) {
		if (bit < 1 && bit > 8) {
			throw new IllegalArgumentException("wrong bit(1-8):" + bit);
		}
		byte[] data = socketCmd(READ_ONOFF);
		return byteTo8Bit(data[9]).charAt(8-bit) == '1';
	}

	private byte[] socketCmd(byte[] cmd) {
		OutputStream os = null;
		InputStream is = null;
		Socket socket = null;
		try {
			debug(Arrays.toString(cmd));
			socket = new Socket(ip, port);
			os = socket.getOutputStream();
			os.write(cmd);
			os.flush();
			is = socket.getInputStream();
			byte data[] = new byte[10];
			int length = is.read(data);
			if (length < 10) {
//				throw new IllegalStateException("error:"+length);
			}
			debug(Arrays.toString(data));
			return data;
		} catch (IOException e) {
			throw new RuntimeException(e);

		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
				}
			}
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
				}
			}
			if(socket != null){
				try {
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	private String byteTo8Bit(byte b) {
		int n = ((int) b) & 0xff;
//		n = n << 8;
		return strTo8Bit(Integer.toBinaryString(n));
	}

	private String strTo8Bit(String str) {
		String res = str;
		for (int i = 0; i < (8 - str.length()); i++) {
			res = "0" + res;
		}
		debug(res);
		return res;
	}
}